package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.service.IMsgRemindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @创建人 liangzai
 * @创建时间 2022/10/21
 * @描述 数据门户平台消息提醒服务实现类
 */
@Slf4j
@Service
public class MsgRemindServiceImpl implements IMsgRemindService {
    /*@Autowired
    BaseProperties baseProperties;*/

    /**
     * 发送消息提醒
     *
     * @param userId  用户id
     * @param content 提醒内容
     * @return 状态 0-成功 1-失败
     */

    @Override
    public int sendMsgRemind(String userId, String title,String content) {
        log.info("【发送消息提醒  用户id={} ,提醒内容 = {}】", userId, content);
        //String cloudServer = baseProperties.getResultOaServer();
        //String url = cloudServer + "/gateway/cloud-system-manage//api/announcement/addSendMessage";
        //String url = "http://127.0.0.1:8899/seeyon/rest/api/announcement/addSendMessage";
        String url = "http://127.0.0.1:8080/gateway/cloud-system-manage/api/announcement/addSendMessage";
        HashMap<String, Object> map = new HashMap();
        map.put("senderId", "admin");
        map.put("title", title);
        map.put("detailContent", content);
        map.put("receiverId", userId);
        map.put("isModel", 0);
        map.put("isSend", 1);
        map.put("isTimer", 0);
        map.put("msgType", 0);
        map.put("timeSend", 1);

        log.info("请求参数 ={} ", map);
        HashMap<String, String> headers = new HashMap<>();//存放请求头，可以存放多个请求头
        String Cookie = "TENANCY_ID_21232f297a57a5a743894a0e4a801fc3=; cloudSessionId=Yjk5OWY0OThlYjI4NDM5MGI4OTNkOTgzNjIyN2EyYTk=; accessToken=eyJhbGciOiJIUzI1NiIsInppcCI6IkRFRiJ9.eNpcyk0KhDAMQOG7ZG0XsWPaepkhtelQwR-0giDeXaELZZbv4x2wbh5a4DCkESrYVlm-KfzLyIO8bFp-ZcI7gsz5KdlnaJHIUWMQ6woS5wK1K9DndL9ae9YcvRJro_p0TpQligpJIrHpxBsH5wUAAP__.-_S_ty01OqocdGygv_BRMuZngpoDxh8UiMcCndYkSV8";
                headers.put("Cookie", Cookie);
         String result = HttpUtil.createPost(url).addHeaders(headers).form(map).execute().body();
        //String result = HttpUtil.createPost(url).form(map).execute().body();

        if (StrUtil.isBlank(result)){
            throw new RuntimeException("【接口异常】");
        }
         if (getJSONType(result)){
             JSONObject resultObj = JSONObject.parseObject(result);
             return  1 == resultObj.getIntValue("status") ? 0 : 1 ;
         }    else {
             log.error("【返回结果不是json格式】");
             return 1;
         }
    }

    /**
     * 校验字符串是都是json格式
     * @param str
     * @return
     */
    public static boolean getJSONType(String str) {
        boolean result = false;
        if (StrUtil.isNotBlank(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }

}
